package com.musifind.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fizy.client.objects.Album;
import com.fizy.client.objects.Track;
import com.musifind.activities.R;
import com.musifind.common.RoundedTransformation;
import com.musifind.models.SearchItems;
import com.squareup.picasso.Picasso;

public class SearchAdapterTracks extends ArrayAdapter<SearchItems> {

	Context context;
	int layoutResourceId;
	ArrayList<SearchItems> objects = new ArrayList<SearchItems>();
	SearchItems searchItems;
	Album album;

	public SearchAdapterTracks(Context context, int layoutResourceId, ArrayList<SearchItems> objects) {
		super(context, layoutResourceId, objects);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.objects = objects;
	}

	public static class SearchHolder {
		ImageView imgAlbumCover;
		TextView txtTrackName;
		TextView txtArtistName;
		TextView txtAlbumName;
		TextView txtAlbumYear;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		View row = convertView;
		Track track = new Track();
		album = new Album();
		SearchHolder holder;

		if (row == null) {

			LayoutInflater inflater = LayoutInflater.from(context);
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new SearchHolder();
			holder.imgAlbumCover = (ImageView) row.findViewById(R.id.imgAlbumCover);
			holder.txtTrackName = (TextView) row.findViewById(R.id.txtTrackName);
			holder.txtArtistName = (TextView) row.findViewById(R.id.txtArtistName);
			holder.txtAlbumName = (TextView) row.findViewById(R.id.txtAlbumName);
			holder.txtAlbumYear = (TextView) row.findViewById(R.id.txtAlbumYear);
			row.setTag(holder);
		}

		holder = (SearchHolder) row.getTag();

		final SearchItems sItems = objects.get(position);

		// holder.imgAlbumCover.setImageBitmap(GeneralHelper.roundCornerImage(
		// GeneralHelper.getBitmapFromURL(sItems.getCoverImage()), 90));
		// holder.imgAlbumCover.setImageBitmap(GeneralHelper.getBitmapFromURL(sItems.getCoverImage()));
		holder.txtTrackName.setText(sItems.getTrackName());
		holder.txtArtistName.setText(sItems.getArtistName());
		holder.txtAlbumName.setText(sItems.getAlbumName());
		holder.txtAlbumYear.setText(sItems.getAlbumYear());

		// Trigger the download of the URL asynchronously into the image view.
		Picasso.with(context).load(sItems.getCoverImage()).transform(new RoundedTransformation(110, 0))
				.into(holder.imgAlbumCover);

		return row;

	}
	// @Override
	// public int getItemViewType(int position) {
	// return position;
	// }
	//
	// @Override
	// public int getViewTypeCount() {
	// return 500;
	// }
}
